namespace Crux.Core.Commands;
/// <summary>
/// 带有数据返回的Result
/// </summary>
/// <typeparam name="T"></typeparam>
public class CommandResult<T> : CommandResult
{
    /// <summary>
    /// 数据
    /// </summary>
    /// <value></value>
    public T Payload { get; init; }
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="payload">数据</param>
    /// <param name="success">是否成功</param>
    /// <param name="message">信息</param>
    protected CommandResult(
        T payload,
        bool success = true,
        string? message = null)
    {
        if (!success && string.IsNullOrEmpty(message))
            throw new ArgumentNullException("message must be not null");

        Payload = payload;
        Success = success;
        Message = message;
    }
    /// <summary>
    /// 成功
    /// </summary>
    /// <param name="payload"></param>
    /// <returns></returns>
    public static CommandResult<T> Successed(T payload)
        => new CommandResult<T>(payload: payload);
    /// <summary>
    /// 失败
    /// </summary>
    /// <param name="payload"></param>
    /// <param name="message"></param>
    /// <returns></returns>
    public static CommandResult<T> Failed(T payload, string message)
        => new CommandResult<T>(payload: payload, success: false, message: message);
    /// <summary>
    /// 解构
    /// </summary>
    /// <param name="success"></param>
    /// <param name="message"></param>
    /// <param name="payload"></param>
    public void Deconstruct(out bool success, out string? message, out T payload)
    {
        success = Success;
        message = Message;
        payload = Payload;
    }

    /// <summary>
    /// 解构
    /// </summary>
    /// <param name="success"></param>
    /// <param name="payload"></param>
    public void Deconstruct(out bool success, out T payload)
    {
        success = Success;
        payload = Payload;
    }
}
